package cc.admin.modules.graduate.service.impl;

import cc.admin.modules.graduate.entity.*;
import cc.admin.modules.graduate.entity.VO.ExamPaperEnglishVo;
import cc.admin.modules.graduate.mapper.*;
import cc.admin.modules.graduate.service.IExamPaperEnglishService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 试卷
 * @Author: cc-admin
 * @Date: 2025-02-21
 * @Version: V1.0.0
 */
@Service
public class ExamPaperEnglishServiceImpl extends ServiceImpl<ExamPaperEnglishMapper, ExamPaperEnglish> implements IExamPaperEnglishService {

    @Autowired
    private DesignEnglishQuestionsListMapper designEnglishQuestionsListMapper;

    @Autowired
    private DesignEnglishQuestionsMapper designEnglishQuestionsMapper;

    @Autowired
    private SelectFiveMapper selectFiveMapper;

    @Autowired
    private ExamPaperEnglishMapper examPaperEnglishMapper;

    @Autowired
    private WriteQuestionsMapper writeQuestionsMapper;

    @Override
    public void saveExamPaperEnglish(ExamPaperEnglish examPaperEnglish, String id) {
        String blankNum = examPaperEnglish.getBlankNum();
        String yuedukNum = examPaperEnglish.getYuedukNum();
        String selectNum = examPaperEnglish.getSelectNum();
        String translateNum = examPaperEnglish.getTranslateNum();
        String writeNum = examPaperEnglish.getWriteNum();


        if (!blankNum.equals("0")) {
            LambdaQueryWrapper<DesignEnglishQuestionsList> designQueryWrapper = new LambdaQueryWrapper<>();
            designQueryWrapper.last("ORDER BY RAND() LIMIT " + blankNum);
            designQueryWrapper.eq(DesignEnglishQuestionsList::getType, "1");
            List<DesignEnglishQuestionsList> designEnglishQuestionsLists = designEnglishQuestionsListMapper.selectList(designQueryWrapper);
            // 所有数据id
            List<String> ids = designEnglishQuestionsLists.stream().map(DesignEnglishQuestionsList::getId).collect(Collectors.toList());
            examPaperEnglish.setBlankIds(StringUtils.join(ids, ","));
        }
        if (!yuedukNum.equals("0")) {
            LambdaQueryWrapper<DesignEnglishQuestionsList> designQueryWrapper = new LambdaQueryWrapper<>();
            designQueryWrapper.last("ORDER BY RAND() LIMIT " + yuedukNum);
            designQueryWrapper.eq(DesignEnglishQuestionsList::getType, "2");
            List<DesignEnglishQuestionsList> designEnglishQuestionsLists = designEnglishQuestionsListMapper.selectList(designQueryWrapper);
            List<String> ids = designEnglishQuestionsLists.stream().map(DesignEnglishQuestionsList::getId).collect(Collectors.toList());
            examPaperEnglish.setYueduIds(StringUtils.join(ids, ","));
        }
        if (!selectNum.equals("0")) {
            LambdaQueryWrapper<SelectFive> selectQueryWrapper = new LambdaQueryWrapper<>();
            selectQueryWrapper.last("ORDER BY RAND() LIMIT " + selectNum);
            selectQueryWrapper.eq(SelectFive::getType, "3");
            List<SelectFive> selectEnglishQuestionsLists = selectFiveMapper.selectList(selectQueryWrapper);
            List<String> ids = selectEnglishQuestionsLists.stream().map(SelectFive::getId).collect(Collectors.toList());
            examPaperEnglish.setSelectIds(StringUtils.join(ids, ","));
        }
        if (!translateNum.equals("0")) {
            LambdaQueryWrapper<DesignEnglishQuestionsList> designQueryWrapper = new LambdaQueryWrapper<>();
            designQueryWrapper.last("ORDER BY RAND() LIMIT " + translateNum);
            designQueryWrapper.eq(DesignEnglishQuestionsList::getType, "4");
            List<DesignEnglishQuestionsList> designEnglishQuestionsLists = designEnglishQuestionsListMapper.selectList(designQueryWrapper);
            List<String> ids = designEnglishQuestionsLists.stream().map(DesignEnglishQuestionsList::getId).collect(Collectors.toList());
            examPaperEnglish.setTranslateIds(StringUtils.join(ids, ","));
        }
        // 写作部分录入
        if (!writeNum.equals("0")) {
            LambdaQueryWrapper<WriteQuestions> writeQueryWrapper = new LambdaQueryWrapper<>();
            writeQueryWrapper.last("ORDER BY RAND() LIMIT " + writeNum);
            writeQueryWrapper.eq(WriteQuestions::getType, "5");
            List<WriteQuestions> writeQuestionsLists = writeQuestionsMapper.selectList(writeQueryWrapper);
            List<String> ids = writeQuestionsLists.stream().map(WriteQuestions::getId).collect(Collectors.toList());
            examPaperEnglish.setWriteIds(StringUtils.join(ids, ","));
        }
        this.save(examPaperEnglish);
    }

    @Override
    public ExamPaperEnglishVo getexampaper(String id) {
        JSONObject jsonObject = JSONObject.parseObject(id);
        id = jsonObject.getString("id");
        ExamPaperEnglishVo examPaperEnglishVo = new ExamPaperEnglishVo();
        examPaperEnglishVo.setId(id);
        ExamPaperEnglish paperEnglish = examPaperEnglishMapper.selectById(id);
        List<String> blankIds = null;
        if (paperEnglish.getBlankIds() != null) {
            blankIds = Arrays.stream(paperEnglish.getBlankIds().split(",")).collect(Collectors.toList());
            examPaperEnglishVo.setBlankIds(paperEnglish.getBlankIds());
        }
        List<String> yueduIds = null;
        if (paperEnglish.getYueduIds() != null) {
            yueduIds = Arrays.stream(paperEnglish.getYueduIds().split(",")).collect(Collectors.toList());
            examPaperEnglishVo.setYueduIds(paperEnglish.getYueduIds());
        }
        List<String> selectIds = null;
        if (paperEnglish.getSelectIds() != null) {
            selectIds = Arrays.stream(paperEnglish.getSelectIds().split(",")).collect(Collectors.toList());
            examPaperEnglishVo.setSelectIds(paperEnglish.getSelectIds());
        }
        List<String> translateIds = null;
        if (paperEnglish.getTranslateIds() != null) {
            translateIds = Arrays.stream(paperEnglish.getTranslateIds().split(",")).collect(Collectors.toList());
            examPaperEnglishVo.setTranslateIds(paperEnglish.getTranslateIds());
        }
        List<String> writeIds = null;
        if (paperEnglish.getWriteIds() != null) {
            writeIds = Arrays.stream(paperEnglish.getWriteIds().split(",")).collect(Collectors.toList());
            examPaperEnglishVo.setWriteIds(paperEnglish.getWriteIds());
        }

        examPaperEnglishVo.setPaperName(paperEnglish.getPaperName());

        if (blankIds!=null) {
            List<DesignEnglishQuestionsList> blankQuestions = designEnglishQuestionsListMapper.selectBatchIds(blankIds);
            blankQuestions.forEach(designEnglishQuestionsList -> {
                List<String> childIds = Arrays.stream(designEnglishQuestionsList.getChildIds().split(",")).collect(Collectors.toList());
                LambdaQueryWrapper<DesignEnglishQuestions> queryWrapper = new LambdaQueryWrapper<>();
                // 添加 id 在指定列表中的条件
                queryWrapper.in(DesignEnglishQuestions::getId, childIds);
                // 按 index_num 字段升序排序
                queryWrapper.orderByAsc(DesignEnglishQuestions::getIndexNum);
                // 执行查询
                List<DesignEnglishQuestions> childQuestions = designEnglishQuestionsMapper.selectList(queryWrapper);
                ;
                designEnglishQuestionsList.setQuestionList(childQuestions);
            });
            examPaperEnglishVo.setBlankQuestionsList(blankQuestions);
        }
        if (yueduIds!=null) {
            List<DesignEnglishQuestionsList> yueduQuestions = designEnglishQuestionsListMapper.selectBatchIds(yueduIds);
            yueduQuestions.forEach(designEnglishQuestionsList -> {
                List<String> childIds = Arrays.stream(designEnglishQuestionsList.getChildIds().split(",")).collect(Collectors.toList());
                LambdaQueryWrapper<DesignEnglishQuestions> queryWrapper = new LambdaQueryWrapper<>();
                // 添加 id 在指定列表中的条件
                queryWrapper.in(DesignEnglishQuestions::getId, childIds);
                // 按 index_num 字段升序排序
                queryWrapper.orderByAsc(DesignEnglishQuestions::getIndexNum);
                // 执行查询
                List<DesignEnglishQuestions> childQuestions = designEnglishQuestionsMapper.selectList(queryWrapper);
                ;
                designEnglishQuestionsList.setQuestionList(childQuestions);
            });
            examPaperEnglishVo.setYueduQuestionsList(yueduQuestions);

        }
        if (selectIds!=null) {
            List<SelectFive> selectQuestions = selectFiveMapper.selectBatchIds(selectIds);
            selectQuestions.forEach(selectFive -> {
                List<String> orderNode = Arrays.stream(selectFive.getOrderNodeString().split(",")).collect(Collectors.toList());
                selectFive.setOrderNode(orderNode);
            });
            examPaperEnglishVo.setSelectQuestionsList(selectQuestions);
        }
        if (translateIds!=null) {
            List<DesignEnglishQuestionsList> translateQuestions = designEnglishQuestionsListMapper.selectBatchIds(translateIds);
            translateQuestions.forEach(designEnglishQuestionsList -> {
                List<String> childIds = Arrays.stream(designEnglishQuestionsList.getChildIds().split(",")).collect(Collectors.toList());
                LambdaQueryWrapper<DesignEnglishQuestions> queryWrapper = new LambdaQueryWrapper<>();
                // 添加 id 在指定列表中的条件
                queryWrapper.in(DesignEnglishQuestions::getId, childIds);
                // 按 index_num 字段升序排序
                queryWrapper.orderByAsc(DesignEnglishQuestions::getIndexNum);
                // 执行查询
                List<DesignEnglishQuestions> childQuestions = designEnglishQuestionsMapper.selectList(queryWrapper);
                ;
                designEnglishQuestionsList.setQuestionList(childQuestions);
            });
            examPaperEnglishVo.setTranslateQuestionsList(translateQuestions);
        }
        if (writeIds!=null) {
            List<WriteQuestions> writeQuestions = writeQuestionsMapper.selectBatchIds(writeIds);
            examPaperEnglishVo.setWriteQuestionsList(writeQuestions);
        }
        return examPaperEnglishVo;
    }

    @Override
    public ExamPaperEnglishVo getexampaper2(String id) {
        ExamPaperEnglishVo examPaperEnglishVo = new ExamPaperEnglishVo();
        examPaperEnglishVo.setId(id);
        ExamPaperEnglish paperEnglish = examPaperEnglishMapper.selectById(id);
        List<String> blankIds = null;
        if (paperEnglish.getBlankIds() != null) {
            blankIds = Arrays.stream(paperEnglish.getBlankIds().split(",")).collect(Collectors.toList());
            examPaperEnglishVo.setBlankIds(paperEnglish.getBlankIds());
        }
        List<String> yueduIds = null;
        if (paperEnglish.getYueduIds() != null) {
            yueduIds = Arrays.stream(paperEnglish.getYueduIds().split(",")).collect(Collectors.toList());
            examPaperEnglishVo.setYueduIds(paperEnglish.getYueduIds());
        }
        List<String> selectIds = null;
        if (paperEnglish.getSelectIds() != null) {
            selectIds = Arrays.stream(paperEnglish.getSelectIds().split(",")).collect(Collectors.toList());
            examPaperEnglishVo.setSelectIds(paperEnglish.getSelectIds());
        }
        List<String> translateIds = null;
        if (paperEnglish.getTranslateIds() != null) {
            translateIds = Arrays.stream(paperEnglish.getTranslateIds().split(",")).collect(Collectors.toList());
            examPaperEnglishVo.setTranslateIds(paperEnglish.getTranslateIds());
        }
        List<String> writeIds = null;
        if (paperEnglish.getWriteIds() != null) {
            writeIds = Arrays.stream(paperEnglish.getWriteIds().split(",")).collect(Collectors.toList());
            examPaperEnglishVo.setWriteIds(paperEnglish.getWriteIds());
        }

        examPaperEnglishVo.setPaperName(paperEnglish.getPaperName());

        if (blankIds!=null) {
            List<DesignEnglishQuestionsList> blankQuestions = designEnglishQuestionsListMapper.selectBatchIds(blankIds);
            blankQuestions.forEach(designEnglishQuestionsList -> {
                List<String> childIds = Arrays.stream(designEnglishQuestionsList.getChildIds().split(",")).collect(Collectors.toList());
                LambdaQueryWrapper<DesignEnglishQuestions> queryWrapper = new LambdaQueryWrapper<>();
                // 添加 id 在指定列表中的条件
                queryWrapper.in(DesignEnglishQuestions::getId, childIds);
                // 按 index_num 字段升序排序
                queryWrapper.orderByAsc(DesignEnglishQuestions::getIndexNum);
                // 执行查询
                List<DesignEnglishQuestions> childQuestions = designEnglishQuestionsMapper.selectList(queryWrapper);
                ;
                designEnglishQuestionsList.setQuestionList(childQuestions);
            });
            examPaperEnglishVo.setBlankQuestionsList(blankQuestions);
        }
        if (yueduIds!=null) {
            List<DesignEnglishQuestionsList> yueduQuestions = designEnglishQuestionsListMapper.selectBatchIds(yueduIds);
            yueduQuestions.forEach(designEnglishQuestionsList -> {
                List<String> childIds = Arrays.stream(designEnglishQuestionsList.getChildIds().split(",")).collect(Collectors.toList());
                LambdaQueryWrapper<DesignEnglishQuestions> queryWrapper = new LambdaQueryWrapper<>();
                // 添加 id 在指定列表中的条件
                queryWrapper.in(DesignEnglishQuestions::getId, childIds);
                // 按 index_num 字段升序排序
                queryWrapper.orderByAsc(DesignEnglishQuestions::getIndexNum);
                // 执行查询
                List<DesignEnglishQuestions> childQuestions = designEnglishQuestionsMapper.selectList(queryWrapper);
                ;
                designEnglishQuestionsList.setQuestionList(childQuestions);
            });
            examPaperEnglishVo.setYueduQuestionsList(yueduQuestions);

        }
        if (selectIds!=null) {
            List<SelectFive> selectQuestions = selectFiveMapper.selectBatchIds(selectIds);
            selectQuestions.forEach(selectFive -> {
                List<String> orderNode = Arrays.stream(selectFive.getOrderNodeString().split(",")).collect(Collectors.toList());
                selectFive.setOrderNode(orderNode);
            });
            examPaperEnglishVo.setSelectQuestionsList(selectQuestions);
        }
        if (translateIds!=null) {
            List<DesignEnglishQuestionsList> translateQuestions = designEnglishQuestionsListMapper.selectBatchIds(translateIds);
            translateQuestions.forEach(designEnglishQuestionsList -> {
                List<String> childIds = Arrays.stream(designEnglishQuestionsList.getChildIds().split(",")).collect(Collectors.toList());
                LambdaQueryWrapper<DesignEnglishQuestions> queryWrapper = new LambdaQueryWrapper<>();
                // 添加 id 在指定列表中的条件
                queryWrapper.in(DesignEnglishQuestions::getId, childIds);
                // 按 index_num 字段升序排序
                queryWrapper.orderByAsc(DesignEnglishQuestions::getIndexNum);
                // 执行查询
                List<DesignEnglishQuestions> childQuestions = designEnglishQuestionsMapper.selectList(queryWrapper);
                ;
                designEnglishQuestionsList.setQuestionList(childQuestions);
            });
            examPaperEnglishVo.setTranslateQuestionsList(translateQuestions);
        }
        if (writeIds!=null) {
            List<WriteQuestions> writeQuestions = writeQuestionsMapper.selectBatchIds(writeIds);
            examPaperEnglishVo.setWriteQuestionsList(writeQuestions);
        }
        return examPaperEnglishVo;
    }
}
